package com.touch.core.script;
import java.io.*;
import java.util.*;
import javax.script.*;
public class ScriptManager 
{
	private final ScriptEngineManager mgr;
	private final ScriptEngine engine;
	public ScriptManager(final String name)
	{
		mgr = new ScriptEngineManager();
		engine = mgr.getEngineByName(name);
	}
	public ScriptEngine getEngine()
	{
		return engine;
	}
	public void importScript(final InputStream is) throws Exception
	{
		final InputStreamReader reader = new InputStreamReader(is);
		
		this.engine.eval(reader);
	}
	public void addStaticClass(String alias, Object obj)
	{
		this.engine.put(alias, obj);
	}	
	public String toString()
	{
		return engine.getFactory().getEngineName();
	}	
	public static Vector <String> listScriptingEnginesByName() 
	{
	 final Vector <String> names = new Vector <String>();		 	
	    final ScriptEngineManager mgr = new ScriptEngineManager();	        
	    for (ScriptEngineFactory factory : mgr.getEngineFactories()) 
	    {
	        for (String name : factory.getNames()) 	              
	            names.add(name);	         
	    }
	    return names;
	}
}
